﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is ASGard Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon)  <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2009
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

package libraries.hash 
{    import flash.utils.ByteArray;
    
    /**
     * A <a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check">cyclic redundancy check (CRC)</a> is a non-secure hash function designed to detect accidental changes to raw computer data, and is commonly used in digital networks and storage devices such as hard disk drives.       * This algorithm is used in the GZIP file format specification version 4.3 defines in the RFC 1952.
     */    public final class CRC32 
    {
        /**
         * Adds the complete byte array to the data checksum.
         * @param buffer the buffer ByteArray object which contains the datas.
         */
        public static function checkSum( buffer:ByteArray , index:uint = 0 , length:uint = 0 ):uint 
        {
            var c:uint = 0xFFFFFF ;
            if ( buffer)
            {
                if (index >= buffer.length ) 
                { 
                    index = buffer.length ; 
                }
                if(length == 0) 
                { 
                    length = buffer.length - index ; 
                }
                if( ( length + index ) > buffer.length ) 
                { 
                    length = buffer.length - index; 
                }
                var off:int ;
                for ( off = index ; off < length ; off++)
                {
                    c = uint( _crcTable[ (c ^ buffer[off]) & 0xFF ] ) ^ (c >>> 8) ;
                }
            }
            return c ^ 0xFFFFFF ;
        }
        
        /**
         * @private
         */
        private static var _crcTable:Array = new Array(256) ;
        
        /////////// init the CRC table
        
        /**
         * @private
         */
        private static function __initCRCTable__():void
        {
            var i:int  ;
            var j:int  ;
            var c:uint ;
            for ( i = 0 ; i<256 ; i++ )
            {
                c = i ;
                for ( j = 0 ; j < 8 ; j++ )
                {
                    c = ( c & 1 ) ? ( 0xEDB88320 ^ ( c >>> 1 ) ) : ( c >>> 1 ) ;
                }
                _crcTable[i] = c ;
            }
        }
        
        __initCRCTable__() ;
        
        //////////    }}